<?php

namespace app\controller\admin\yys;

use app\model\api\YysHero;
use app\model\api\YysSource;
use laytp\controller\Backend;
use laytp\library\CommonFun;
use think\facade\Db;

/**
 * Class Role
 * @package app\controller\admin\yys
 */
class Role extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法

    public $orderRule = ['sort' => 'desc','id' => 'desc'];

    public function _initialize()
    {
        $this->model = new YysHero();
    }


    //添加
    public function add()
    {
        $post     = CommonFun::filterPostData($this->request->post());

        $source = (new YysSource())->where('type','=','jy')
            ->cache(true)->column('ico','name');

        $insData['name'] = $post['name'];
        $insData['icon']  = $post['icon'];
        $insData['img']     = $post['img'];
        $insData['category']      = $post['category'];
        $insData['quality']     = $post['quality'];
        $insData['position']     = $post['position'];
        $insData['desc']     = $post['desc'];
        $insData['end_attribute']     = json_encode(array($post['max_attack'],$post['max_life'],$post['max_defense'],$post['max_speed'],$post['max_crit']),JSON_UNESCAPED_UNICODE);
        $insData['start_attribute']     = json_encode(array($post['min_attack'],$post['min_life'],$post['min_defense'],$post['min_speed'],$post['min_crit']),JSON_UNESCAPED_UNICODE);
        $insData['start_ability']     = implode('-',array($post['before_attack_grade'],$post['before_life_grade'],$post['before_defense_grade'],$post['before_speed_grade'],$post['before_crit_grade']));
        $insData['end_ability']     = implode('-',array($post['after_attack_grade'],$post['after_life_grade'],$post['after_defense_grade'],$post['after_speed_grade'],$post['after_crit_grade']));
        $pvpData = [];
        $pveData = [];

        $pvp = explode(',',$post['pvp']);
        foreach($pvp as $item){
            if(empty($item)){
                continue;
            }
            $pvpData[] = array(
                'name' => $item,
                'img' => $source[$item]
            );
        }
        $pve = explode(',',$post['pve']);
        foreach($pve as $item){
            if(empty($item)){
                continue;
            }
            $pveData[] = array(
                'name' => $item,
                'img' => $source[$item]
            );
        }

        $insData['pvp'] = json_encode($pvpData,JSON_UNESCAPED_UNICODE);
        $insData['pve'] = json_encode($pveData,JSON_UNESCAPED_UNICODE);
        $insData['create_time'] = time();

        try {
            $save = $this->model->save($insData);
            if (!$save) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());

        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');

        $source = (new YysSource())->where('type','=','jy')
            ->cache(true)->column('ico','name');

        $pvpData = [];
        $pveData = [];
        $pvp = explode(',',$post['pvp']);
        foreach($pvp as $item){
            if(empty($item)){
                continue;
            }
            $pvpData[] = array(
                'name' => $item,
                'img' => $source[$item]
            );
        }
        $pve = explode(',',$post['pve']);
        foreach($pve as $item){
            if(empty($item)){
                continue;
            }
            $pveData[] = array(
                'name' => $item,
                'img' => $source[$item]
            );
        }

        $row->name    = $post['name'];
        $row->icon     = $post['icon'];
        $row->img        = $post['img'];
        $row->category         = $post['category'];
        $row->quality        = $post['quality'];
        $row->position        = $post['position'];
        $row->desc        = $post['desc'];
        $row->end_attribute        = json_encode(array($post['max_attack'],$post['max_life'],$post['max_defense'],$post['max_speed'],$post['max_crit']),JSON_UNESCAPED_UNICODE);
        $row->start_attribute        = json_encode(array($post['min_attack'],$post['min_life'],$post['min_defense'],$post['min_speed'],$post['min_crit']),JSON_UNESCAPED_UNICODE);
        $row->start_ability        = implode('-',array($post['before_attack_grade'],$post['before_life_grade'],$post['before_defense_grade'],$post['before_speed_grade'],$post['before_crit_grade']));
        $row->end_ability        = implode('-',array($post['after_attack_grade'],$post['after_life_grade'],$post['after_defense_grade'],$post['after_speed_grade'],$post['after_crit_grade']));
        $row->pvp        = json_encode($pvpData,JSON_UNESCAPED_UNICODE);
        $row->pve        = json_encode($pveData,JSON_UNESCAPED_UNICODE);;

        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }



    public function info(){
        $id = $this->request->param('id');

        $role = $this->model->where('id',$id)->find()->toArray();
        list($role['min_attack'],$role['min_life'],$role['min_defense'],$role['min_speed'],$role['min_crit']) = json_decode($role['start_attribute'],true);
        list($role['max_attack'],$role['max_life'],$role['max_defense'],$role['max_speed'],$role['max_crit']) = json_decode($role['end_attribute'],true);
        list($role['before_attack_grade'],$role['before_life_grade'],$role['before_defense_grade'],$role['before_speed_grade'],$role['before_crit_grade']) = explode('-',$role['start_ability']);
        list($role['after_attack_grade'],$role['after_life_grade'],$role['after_defense_grade'],$role['after_speed_grade'],$role['after_crit_grade']) = explode('-',$role['end_ability']);
        $role['pvp']   = !empty($role['pvp'])?implode(',',array_column(json_decode($role['pvp'],true),'name')):'';
        $role['pve']   = !empty($role['pve'])?implode(',',array_column(json_decode($role['pve'],true),'name')):'';
        $role['desc']  = !empty($role['desc'])?$role['desc']:'';

        return $this->success('获取成功', $role);
    }

}